package _9练习类;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClockFrame extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClockFrame frame = new ClockFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClockFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 45));
		contentPane.add(lblNewLabel);
		
		// 死循环会造成 ClockFrame()构造方法 一直无法运行结束，所以窗体不会显示
		// 且死循环不能写在主线程中，因为界面的绘制刷新也是在主线程，
//		while(true)
//		{
//			Date d1 = new Date();
//			DateFormat df = new SimpleDateFormat("HH:mm:ss");
//			lblNewLabel.setText(df.format(d1));
//			
//		}
		
		// 新建一个线程
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				
				// 在线程中定时执行代码
				while(true)
				{
					Date d1 = new Date();
					DateFormat df = new SimpleDateFormat("HH:mm:ss");
					lblNewLabel.setText(df.format(d1));
					
				}
				
			}
		}).start();// start() 启动线程

	}

}